package com.hqyj.service.impl;

import com.hqyj.exception.R;
import com.hqyj.pojo.*;
import com.hqyj.dao.OrderInfoMapper;
import com.hqyj.service.AliPayService;
import com.hqyj.service.OrderDetailService;
import com.hqyj.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2023-01-28 09:21:30
 */
@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {


    @Resource
    OrderInfoMapper orderInfoMapper;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    AliPayService aliPayService;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Transactional
    @Override
    public R createOrder(OrderInfoParameter orderInfoParameter, HttpServletRequest request) {
        log.info("----创建订单信息-----");
        //创建订单信息对象
        OrderInfo orderInfo = new OrderInfo();
        //获取买家id
        String token =  request.getHeader("Authorization");
        String buyId = JwtUtil.getAudience(token);
        //设置买家id
        orderInfo.setBuyerId(buyId);
        //设置订单金额
        orderInfo.setOrderMoney(orderInfoParameter.getOrderMoney());
        //新增订单信息
        int num =  orderInfoMapper.insert(orderInfo);
        if (num >0){
            log.info("----创建订单明细信息-----");
            List<OrderDetail> list = new LinkedList<>();
            for(Item item:orderInfoParameter.getList()){
                //创建订单明细对象
                OrderDetail orderDetail = new OrderDetail();
                //设置订单id
                orderDetail.setOrderId(orderInfo.getOrderId());
                //设置卖家Id
                orderDetail.setSellerId("1");
                orderDetail.setGoodsId(item.getGoodsId());
                //设置商品单价
                orderDetail.setGoodsPrice(item.getGoodsPrice());
                //设置购买商品数量
                orderDetail.setGoodsNum(item.getNumber());
                //设置商品总金额
                orderDetail.setGoodsMoney(item.getMoney());
                list.add(orderDetail);
            }
            //插入订单明细
            boolean is = orderDetailService.saveBatch(list);
            if (is){
                log.info("插入订单成功");
                //清空我的购物车
                log.info("---清空购物车---");
                //---------获取用户名--------
                String userName = JwtUtil.getClaimByName(token,"userName").toString();
                //创建购物车键名
                String key = "buyCar:"+userName;
                //创建商品id的键名
                String keyId = "buyCar:id:"+userName;
                /*--------------------先删除购物车数据--------------------*/
                log.info("---先删除购物车数据----");
                //获取购物车中的商品id信息
                Object obj  = redisTemplate.opsForList().range(keyId,0,-1);
                //转换商品信息
                List<Integer> listId = (List<Integer> )obj;
                for(Integer s:listId){
                    //删除购物车的商品Id
                    redisTemplate.opsForList().remove(keyId,0,s);
                    //删除购物车的商品信息
                    redisTemplate.opsForHash().delete(key,s.toString());
                }

                //成功返回支付的链接
                return R.ok("订单生成成功",null);

            }

        }
        return R.error("订单生成失败");
    }

    @Override
    public R queryOrder(HttpServletRequest request) {
        String token =  request.getHeader("Authorization");
        String buyId = JwtUtil.getAudience(token);
        //查询订单
        List<OrderInfo> list = orderInfoMapper.queryOrderByBuyId(buyId);

        return R.ok("success",list);
    }

    @Override
    public R updatePayStatus(String num) {

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setBuyStatus(1);
        orderInfo.setPayStatus(1);
        orderInfo.setOrderId(num);
        int n =orderInfoMapper.updateById(orderInfo);
        if (n>0){
            return R.ok();
        }
        return R.error();
    }
}
